package com.engine4cloud.tdf.supervision.convert;

import com.engine4cloud.tdf.common.core.util.lambda.Actions;
import com.engine4cloud.tdf.supervision.dto.create.JzdxResumeCreateDTO;
import com.engine4cloud.tdf.supervision.dto.result.DictSelectResultDTO;
import com.engine4cloud.tdf.supervision.dto.result.JzdxResumeResultDTO;
import com.engine4cloud.tdf.supervision.dto.update.JzdxResumeUpdateDTO;
import com.engine4cloud.tdf.supervision.entity.JzdxResumeEntity;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Mapper
public interface JzdxResumeConvert {
    JzdxResumeConvert INSTANCE = Mappers.getMapper(JzdxResumeConvert.class);

    JzdxResumeResultDTO convertTo(JzdxResumeEntity s);

    JzdxResumeEntity createTo(JzdxResumeCreateDTO s);

    JzdxResumeEntity updateTo(JzdxResumeUpdateDTO s);

    JzdxResumeResultDTO convert(JzdxResumeEntity s);

    default JzdxResumeEntity create(JzdxResumeCreateDTO s, Long tenantId) {
        JzdxResumeEntity entity = createTo(s);
        entity.setZw(s.getPositionList().get(s.getPositionList().size() - 1));
        entity.setTenantId(tenantId);

        return entity;
    }

    default JzdxResumeEntity update(JzdxResumeUpdateDTO s) {
        JzdxResumeEntity entity = updateTo(s);
        entity.setZw(s.getPositionList().get(s.getPositionList().size() - 1));
        return entity;
    }

    default JzdxResumeResultDTO convert(JzdxResumeEntity s, List<DictSelectResultDTO> dctJobList) {
        if (Objects.isNull(s) || CollectionUtils.isEmpty(dctJobList)) {
            return null;
        }

        JzdxResumeResultDTO result = convertTo(s);
        String parent = dctJobList.stream().filter(job -> Objects.equals(job.getCode(), s.getZw())).map(DictSelectResultDTO::getPcode).findFirst().orElse(null);
        List<String> jobList = new ArrayList<>();
        Actions.apply(StringUtils.isNotEmpty(parent), () -> jobList.add(parent));
        jobList.add(s.getZw());

        result.setPositionList(jobList);
        return result;
    }
}
